package cn.stylefeng.guns.modular.reimbursement.controller;

import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.bean.BetweenMonth;
import cn.stylefeng.guns.comm.util.BetweenTimeUtil;
import cn.stylefeng.guns.comm.util.DictTypeConstant;
import cn.stylefeng.guns.modular.project.entity.Project;
import cn.stylefeng.guns.modular.project.model.result.ProjectResult;
import cn.stylefeng.guns.modular.project.service.ProjectService;
import cn.stylefeng.guns.modular.reimbursement.entity.Reimbursement;
import cn.stylefeng.guns.modular.reimbursement.entity.custom.CustomReimbursement;
import cn.stylefeng.guns.modular.reimbursement.model.params.ReimbursementParam;
import cn.stylefeng.guns.modular.reimbursement.model.result.ReimbursementResult;
import cn.stylefeng.guns.modular.reimbursement.service.ReimbursementService;
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.entity.Dict;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * 报销表控制器
 *
 * @author
 * @Date 2019-10-14 14:14:59
 */
@Controller
@RequestMapping("/reimbursement")
public class ReimbursementController extends BaseController {

    private String PREFIX = "/reimbursement";

    @Autowired
    private ReimbursementService reimbursementService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private DictService dictService;

    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2019-10-14
     */
    @RequestMapping("")
    public String index() {
        String fromMonthStr = "";
        int fromMonth = (DateUtil.thisMonth() + 1);
        if (fromMonth < 10) {
            fromMonthStr = "0" + String.valueOf(fromMonth);
        } else {
            fromMonthStr = String.valueOf(fromMonth);
        }

        String toMonthStr = "";
        int toMonth = (DateUtil.month(DateUtil.nextMonth()) + 1);
        if (toMonth < 10) {
            toMonthStr = "0" + String.valueOf(toMonth);
        } else {
            toMonthStr = String.valueOf(toMonth);
        }
        String s = DateUtil.thisYear() + "-" + fromMonthStr + " 至 " + DateUtil.year(DateUtil.nextMonth()) + "-" + toMonthStr;
        setAttr("betweenMonthStr", s);
        // 费用类型
        setFylx();
        return PREFIX + "/reimbursement.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2019-10-14
     */
    @RequestMapping("/add")
    public String add() {
        setProjects();
        setFylx();
        return PREFIX + "/reimbursement_add.html";
    }

    private void setFylx() {
        List<Dict> fylx = dictService.listDictsByCode(DictTypeConstant.FYLX);
        setAttr("fylx", fylx);
    }

    private void setProjects() {
        List<ProjectResult> allProject = projectService.findAllProject();
        setAttr("projects", allProject);
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2019-10-14
     */
    @RequestMapping("/edit")
    public String edit() {
        setProjects();
        setFylx();
        return PREFIX + "/reimbursement_edit.html";
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2019-10-14
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ReimbursementParam reimbursementParam) {
        reimbursementParam.setUserId((ShiroKit.getUser().getId()));
        this.reimbursementService.add(reimbursementParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2019-10-14
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ReimbursementParam reimbursementParam) {
        this.reimbursementService.update(reimbursementParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2019-10-14
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ReimbursementParam reimbursementParam) {
        this.reimbursementService.delete(reimbursementParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author
     * @Date 2019-10-14
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ReimbursementParam reimbursementParam) {
        Reimbursement detail = this.reimbursementService.getById(reimbursementParam.getId());
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String time = dateFormat.format(detail.getOccurDate());
//        CustomReimbursement customReimbursement = new CustomReimbursement();
//        BeanUtils.copyProperties(detail, customReimbursement);
//        customReimbursement.setOccurDateYmd(time);
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-10-14
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ReimbursementParam reimbursementParam, String betweenMonthStr) {
        if (betweenMonthStr != null && betweenMonthStr.length() > 0) {
            BetweenMonth betweenMonthBean = BetweenTimeUtil.getBetweenMonth(betweenMonthStr, "至");
            reimbursementParam.setBetweenMonth(betweenMonthBean);
        }
        List<ReimbursementResult> list = this.reimbursementService.findPageBySpec(reimbursementParam).getData();
        List<CustomReimbursement> rtnList = new ArrayList<>();
        for (ReimbursementResult reimbursement : list) {
            CustomReimbursement customReimbursement = new CustomReimbursement();
            BeanUtils.copyProperties(reimbursement, customReimbursement);
            rtnList.add(customReimbursement);
            //记录人
            User user = userService.getById(reimbursement.getUserId());
            if (user != null) {
                customReimbursement.setUserName(user.getName());
            }
            // 项目名称
            if (reimbursement.getProjectId() != null) {
                Project project = projectService.getById(reimbursement.getProjectId());
                customReimbursement.setProjectName(project.getName());
            }
            // 费用类型
            if (ToolUtil.isNotEmpty(reimbursement.getType())) {
                customReimbursement.setType(ConstantFactory.me().getDictsByName("费用类型", reimbursement.getType()));
            }
        }
        LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
        layuiPageInfo.setData(rtnList);
        return layuiPageInfo;
    }

}


